package com.monkeygroup.mygoplus.common.utils.image.handlers;

import android.content.Context;
import android.net.Uri;
import android.widget.ImageView;

import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.backends.pipeline.PipelineDraweeController;
import com.facebook.drawee.view.SimpleDraweeView;
import com.facebook.imagepipeline.common.ResizeOptions;
import com.facebook.imagepipeline.core.ImagePipeline;
import com.facebook.imagepipeline.request.ImageRequest;
import com.facebook.imagepipeline.request.ImageRequestBuilder;
import com.monkeygroup.mygoplus.common.utils.image.ImageHandler;

import java.io.File;

/**
 * Created by lcw on 2017/6/1.
 */

public class FrescoHandler extends ImageHandler {
    public FrescoHandler(Context context) {
        super(context);
    }
    @Override
    public void into(ImageView target) {
        SimpleDraweeView view = (SimpleDraweeView)target;
        if(resize) {
            if(resId!=null){
                view.setImageResource(resId);
            }

            else if(uri!=null){
                view.setImageURI(uri);
                ImageRequest request = ImageRequestBuilder.newBuilderWithSource(uri)
                        .setResizeOptions(new ResizeOptions(width, height))
                        .build();
                PipelineDraweeController controller = (PipelineDraweeController) Fresco.newDraweeControllerBuilder()
                        .setOldController(view.getController())
                        .setImageRequest(request)
                        .build();
                view.setController(controller);
            }

            else if(file!=null){
                Uri uri = Uri.parse("file:"+file.toString());
                view.setImageURI(uri);
                ImageRequest request = ImageRequestBuilder.newBuilderWithSource(uri)
                        .setResizeOptions(new ResizeOptions(width, height))
                        .build();
                PipelineDraweeController controller = (PipelineDraweeController) Fresco.newDraweeControllerBuilder()
                        .setOldController(view.getController())
                        .setImageRequest(request)
                        .build();
                view.setController(controller);
            }
        } else {
            if(resId!=null)
                view.setImageResource(resId);
            else if(uri!=null)
                view.setImageURI(uri);
            else if(file!=null){
                Uri uri = Uri.parse("file:"+file.toString());
                view.setImageURI(uri);
            }
        }
    }

    @Override
    public void invalidate(Uri uri) {
        ImagePipeline imagePipeline = Fresco.getImagePipeline();
        imagePipeline.evictFromMemoryCache(uri);
        imagePipeline.evictFromDiskCache(uri);
    }

    @Override
    public void invalidate(File file) {
        Uri uri = Uri.parse("file:"+file.toString());
        ImagePipeline imagePipeline = Fresco.getImagePipeline();
        imagePipeline.evictFromMemoryCache(uri);
        imagePipeline.evictFromDiskCache(uri);
    }
}
